<?php
class Post extends CI_Controller {    public $articles_pictures;    public function __construct() {
        parent::__construct();
        $this -> output -> enable_profiler(PROFILER);
        is_logged_in();
        $this -> em = $this -> doctrine -> em;        $this -> articles_pictures = realpath(APPPATH . "../post_images");
    }    public function index() {
        $this -> load -> library('table');
        if ($this -> input -> post('search')) {
            if (array_key_exists('search', $_POST)) {
                $term = $this -> input -> post('search');
                $foundPosts = $this -> em -> getRepository('models\Post') -> findBy(array('name' => $term));
            }
            if (count($foundPosts) > 0) {                $data['posts'] = $foundCategories;            }        } else {
            if (PAGINATION_ENABLED) {                $totalOfRows = count($this -> em -> getRepository('models\Post') -> findAll());                $this -> load -> library('pagination');                $config['base_url'] = site_url() . '/post/index';                $config['total_rows'] = $totalOfRows;                $this -> pagination -> initialize($config);
                if ($this -> uri -> segment(3)) {
                    $offset = $this -> uri -> segment(3);
                } else {
                    $offset = 0;
                }
                $posts = $this -> em -> getRepository('models\Post') -> findBy(array(), array(), $this -> pagination -> per_page, $offset);
            } else {                $posts = $this -> em -> getRepository('models\Post') -> findAll();            }            if ($posts) {                $data['posts'] = $posts;            }        }
        $data['main_content'] = "admin/post/index";
        $this -> load -> view("admin/template", $data);
    }    public function create() {
        //load categories for any case
        $qb = $this -> em -> createQueryBuilder();        $qb -> add('select', 'c') -> add('from', 'models\Category c');        $categories = $qb -> getQuery() -> getArrayResult();        $data['categories'] = clean_array_for_dropdown($categories, "id", "name");
        if ($this -> form_validation -> run('post/create') == TRUE) {            try {                $category = $this -> doctrine -> em -> find('models\Category', $this -> input -> post('category_id'));                $user = $this -> doctrine -> em -> find('models\User', $this -> input -> post('author_id'));                $post = new models\Post($user, $category);                if ($_FILES['file']['size'] > 0) {
                    $image_data = $this -> do_upload();
                    $_POST['file'] = $image_data['file_name'];
                } else {                    $_POST['file'] = NULL;                }                models\Post::prepareData($_POST, $post);                $this -> em -> persist($post);                $this -> em -> flush();                $this -> session -> set_flashdata('message', array('title' => 'Post Created', 'content' => 'The post has been sucessfully created', 'type' => ALERT_TYPE_SUCCESS));                redirect('post/index');
            } catch(Exceptions $e) {
                redirect('contact/sorry');
            }
        } else {
            $data['post'] = new models\Post();
            $data['main_content'] = "admin/post/form";
            $this -> load -> view("admin/template", $data);
        }
    }    public function edit($id) {
        $id = (int)$id;
        $data['id'] = $id;        $qb = $this -> em -> createQueryBuilder();        $qb -> add('select', 'c') -> add('from', 'models\Category c');        $categories = $qb -> getQuery() -> getArrayResult();        $data['categories'] = clean_array_for_dropdown($categories, "id", "name");        if ($_SERVER['REQUEST_METHOD'] == 'POST') {            if ($_FILES['file']['size'] > 0) {                $image_data = $this -> do_upload($this -> input -> post('file'));                $_POST['file'] = $image_data['file_name'];            } else {                $_POST['file'] = NULL;            }
            if ($this -> form_validation -> run("post/edit") == TRUE) {                try {                    $post = $this -> doctrine -> em -> find('models\Post', $id);                    models\Post::prepareData($_POST, $post);                    $this -> em -> persist($post);                    $this -> em -> flush();                    $this -> session -> set_flashdata('message', array('title' => 'Post Updated', 'content' => 'The Post has been sucessfully updated', 'type' => ALERT_TYPE_SUCCESS));                    redirect('post/index');                } catch(Exceptions $e) {                    $this -> session -> set_flashdata('message', array('title' => 'Post Not Updated', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_ERROR));                    redirect('post/index');                }            } else {
                $post = new models\Post();                $category = $this -> doctrine -> em -> find('models\Category', $this -> input -> post('category_id'));                $post->setCategory($category);                $post -> setEnabled((isset($_POST['enabled']) == null) ? 0 : 1);                $post -> setFeatured((isset($_POST['featured']) == null) ? 0 : 1);                $post -> setCommentsEnabled((isset($_POST['comments_enabled']) == null) ? 0 : 1);               	$post -> setShortDescription($_POST['short_description']);			    $post -> setArticle($_POST['article']);                $post -> setTitle($_POST['title']);                $post -> setTitleClean($_POST['title_clean']);                $data['post'] = $post;                $data['main_content'] = "admin/post/form";
                $this -> load -> view("admin/template", $data);
            }
        } else {            $data['post'] = $this -> doctrine -> em -> find('models\Post', $id);
            if ($data['post']) {
                $data['main_content'] = "admin/post/form";
                $this -> load -> view("admin/template", $data);
            } else {
                $this -> session -> set_flashdata('message', array('title' => 'Not able to retrieve post registry', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_WARNING));
                redirect('post/index');
            }
        }
    }    public function delete($id) {        try {            $post = $this -> doctrine -> em -> find('models\Post', $id);            $this -> em -> remove($post);            $this -> em -> flush();            $this -> session -> set_flashdata('message', array('title' => 'Post deleted', 'content' => 'The Post has been sucessfully deleted', 'type' => ALERT_TYPE_SUCCESS));        } catch(Exception $e) {            $this -> session -> set_flashdata('message', array('title' => 'Not able to delete post ', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_WARNING));        }        redirect('post/index');    }    public function do_upload($filename = NULL) {        $config['upload_path'] = $this -> articles_pictures;        $config['allowed_types'] = 'gif|jpg|png';        $config['max_size'] = 2097152;        $config['max_width'] = 2048;        $config['max_height'] = 1536;        $config['overwrite'] = TRUE;        $config['file_name'] = (!$filename) ? uniqid() : $filename;        $this -> load -> library('upload', $config);        if (!$this -> upload -> do_upload("file")) {            $error = array('error' => $this -> upload -> display_errors());        } else {            $image_data = $this -> upload -> data();            $config['source_image'] = $image_data['full_path'];            $config['new_image'] = $this -> articles_pictures . '/thumbs';            $config['maintain_ration'] = TRUE;            $config['width'] = 150;            $config['height'] = 100;            $this -> load -> library('image_lib', $config);            $this -> image_lib -> resize();            return $image_data;        }    }}?>